<?php

namespace app\model\goods;

use app\base\BaseModel;

/**
 * 模型-GoodsTypeModel
 * Class GoodsTypeModel
 * @package app\model
 */
class GoodsBuyLogModel extends BaseModel
{
    protected $name = 'goods_buy_log';

    /**添加购买记录
     * @param $buy_log
     * @return bool
     * @throws \Exception
     */
    public static function addBuyLog($buy_log)
    {
        $in_data = [];
        foreach ($buy_log as $value) {
            $in_data[] = [
                'goods_id' => $value['goods_id'],
                'goods_number' => $value['goods_number'],
                'user_id' => $value['user_id'],
                'head_img' => $value['head_img'],
                'nick_name' => $value['nick_name'],
                'add_time' => time()
            ];
        }
        $goods_buy_log_model = new GoodsBuyLogModel();
        $ret = $goods_buy_log_model->saveAll($in_data);
        if ($ret !== false) {
//            库存减少
            return true;
        }
        return false;
    }

    /**
     * 获取列表数据
     * @param $map
     * @param $limit
     * @param bool $repeat
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getListGroup($map, $limit, $repeat = true)
    {
        if ($repeat) {
            $list = self::where($map)->limit($limit)->group('user_id')->select();
        } else {
            $list = self::where($map)->limit($limit)->select();
        }
        return $list;
    }

}
